package connect2congress.client;

import com.google.gwt.widgetideas.graphics.client.Color;
import connect2congress.client.properties.Person;
import connect2congress.client.properties.Verb;
import connect2congress.client.properties.Wnom;

/**
 * Created by IntelliJ IDEA.
 * User: peter
 * Date: Mar 13, 2010
 * Time: 8:21:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class PlotPoint implements Comparable<PlotPoint>{

    private Plot p;

    private Wnom w;
    private Verb v;
    //private Property radius;
    private double radius = 20;
    private Person party;
    private Person name;
    
    private String aname;
    private String aparty;
    private double aw;
    private double av;
    private int id = -1;

    private boolean special;

    private boolean highlighted;

    private int speedFactor = 12;
    private boolean visible;

    private double x;
    private double y;
    private int z = 1;

    private final Color democratColor = new Color(0,0,185);
    private final Color republicanColor = new Color(185,0,0);
    private final Color democratMedColor = new Color(0,0,185,0.2f);
    private final Color republicanMedColor = new Color(185,0,0,0.2f);

    private final Color independentColor = new Color(0,185,185);
    private final Color highlightedDemocratColor = new Color(0,0,255);
    private final Color highlightedRepublicanColor = new Color(255,0,0);
    private final Color highlightedIndependentColor = new Color(0,255,255);

    private final Color totMed = new Color(185,0,185,0.2f);
    public PlotPoint(Plot p) {
        this.p = p;
        this.y = p.height()/2;
        this.x = p.width()/2;
        
        this.visible = true;
        
    }

    public double getW() {

        if(this.isSpecial()) {
            return aw;
        } else if(w != null) {
            return w.getScore();
        } else return 0;
    }

    public void setW(Wnom w) {
        this.w = w;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public boolean isHighlighted() {
        return highlighted;
    }

    public void setHighlighted(boolean highlighted) {
        this.highlighted = highlighted;
    }

    public int getSpeedFactor() {
        return speedFactor;
    }

    public void setSpeedFactor(int speedFactor) {
        this.speedFactor = speedFactor;
    }

    public double getV() {
        if(this.isSpecial()) {
            return av;
        } else if(v != null) {
            return v.getScore();
        } else return 0;
    }

    public void setV(Verb v) {
        this.v = v;
    }

    public String getParty() {
        if(this.aparty != null) {
            return aparty;
        } else
        return party.getParty();
    }

    public void setParty(Person party) {
        this.party = party;
    }

    public String getName() {
        if(this.aname != null) {
            return aname;
        } else return name.getName();
    }

    public void setName(Person name) {
        this.name = name;
    }

    public void setName(String name) {
        this.aname = name;
    }

    public void setParty(String party) {
        this.aparty = party;
    }

    public boolean isSpecial() {
        return special;
    }

    public void setSpecial(boolean special) {
        this.special = special;
    }

    public void setV(double d) {
        this.av = d;
    }
    public void setW(double d) {
        this.aw = d;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getAw() {
        return aw;
    }

    public void setAw(double aw) {
        this.aw = aw;
    }

    public double getAv() {
        return av;
    }

    public void setAv(double av) {
        this.av = av;
    }

    public void draw() {
        if(this.isVisible()) {
            if(w != null && v != null && party != null || this.isSpecial()) {
                p.setStrokeStyle(interpretColor());
                //p.setStrokeStyle(new Color(10,10,10));
                p.setFillStyle(interpretColor());
            //System.out.println(w.getScore());
            //p.setStrokeStyle(c);
                p.beginPath();
                //p.arc(w.getScore(), v.getScore(), radius, 0, (float) Math.PI * 2, false);
                p.arc(x, y, radius/2, 0, (float) Math.PI * 2, false);
                p.closePath();
                if(this.isSpecial()) {
                   p.fill();
                   //p.fillRect(x,y,15,15);
                }
                //p.fill();
                p.stroke();

                if(x < newX()) {
                    x += (newX()-x)/speedFactor;
                //x++;
                } else if(x > newX()) {
                    x -= (x-newX())/speedFactor;
                //	x--;
                }

                if(y < newY()) {
                    y += (newY()-y)/speedFactor;
                    //y++;
                } else if(y > newY()) {
                    y -= (y-newY())/speedFactor;
                    //y--;
                } 
            }
        }
    }
    public boolean under(double x, double y) {
        if(this.x - radius/2 < x && this.x + radius/2 > x && this.y- radius/2 < y && this.y + radius/2 > y) {
            return true;
        } else return false;
    }

    private double newX() {
        double newx = 0;
        newx = (double) ((p.width()/2)-(radius/2));
        if(this.isSpecial()) {
            newx += radius/2 + (double) (newx*-aw);
            //newx = newx;
        } else {
            newx += radius/2 + (double) (newx*-w.getScore());
            //newx = newx;
        }

        return newx;
    }
    private double newY() {
        double newy = p.height()-radius;
        //newy = (p.height()-radius);
        if(this.isSpecial()) {
            newy += radius + newy*(av/Plot.MAX_VERB);
        } else {
            newy += radius + newy*(v.getScore()/Plot.MAX_VERB);
        }
        return radius/2 + newy-p.height();
    }

    public long getId() {
        if(this.isSpecial()) {
            return this.id;
        } else return name.getID();
    }

    private Color interpretColor() {

        if(this.getParty().equals("Democrat")) {
            if(highlighted) {
                 return highlightedDemocratColor;
            } else return democratColor;
        } else if(this.getParty().equals("Republican")) {
            if(highlighted) {
                 return highlightedRepublicanColor;
            } else return republicanColor;
        } else if(this.getParty().equals("Independent")) {
            if(highlighted) {
                return highlightedIndependentColor;
            } else return independentColor;
        } else if(this.getParty().equals("DemMed")) {
            return democratMedColor;
        } else if(this.getParty().equals("RepMed")) {
            return republicanMedColor;
        } else if(this.getParty().equals("TotMed")) {
            return totMed;
        }
            return new Color(0,0,0);
    }

    public Plot getP() {
        return p;
    }

    public void setP(Plot p) {
        this.p = p;
    }

    public int getZ() {
        return z;
    }

    public int compareTo(PlotPoint o) {
        return (int) (this.getId() - o.getId());
    }
}
